package org.arain.baseii.system.oplog;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.arain.baseii.common.CommonServiceImpl;
import org.arain.baseii.grid.GridData;
import org.arain.baseii.grid.QueryParam;
import org.arain.baseii.mapper.SysLogHandleMapper;
import org.arain.baseii.pojo.SysLogHandle;
import org.arain.baseii.pojo.SysLogHandleExample;
import org.arain.baseii.pojo.SysLogHandleExample.Criteria;
import org.arain.baseii.utils.AppUtils;
import org.arain.baseii.utils.DictUtil;
import org.arain.baseii.utils.IDUtil;
import org.arain.baseii.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
/**
 * 
* <p>Title:SysOplogServiceImpl </p>
* @author Arain
* @date2017年9月5日
 */
@Service
@Transactional
public class SysOplogServiceImpl extends CommonServiceImpl<SysOplogDto> implements SysOplogService{
	@Autowired
	private SysLogHandleMapper sysLogHandleMapper;
	@Override
	public void save_01(SysOplogDto dto) {
		dto.setLogId(IDUtil.genId());
		SysLogHandle handle = new SysLogHandle();
		AppUtils.copyProperties(handle, dto);
		handle.setLogTime(new Date());
		sysLogHandleMapper.insert(handle);
	}
	
	@Override
	public String load_grid(QueryParam param) {
		SysOplogDto dto = new SysOplogDto();
		try {
			dto = this.jsonToObject(param.getFilterRules(), dto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JSONArray array = new JSONArray();
		SysLogHandleExample example = new SysLogHandleExample();
		Criteria criteria = example.createCriteria().andOrgidEqualTo(AppUtils.getOrgId());
		if(StringUtils.isNotBlank(param.getOrder()) && StringUtils.isNotBlank(param.getSort())) {
			example.setOrderByClause(param.getSort()+" "+param.getOrder());
		}
		if(StringUtils.isNotBlank(dto.getLogUser())) {
			criteria.andLogUserLike("%"+dto.getLogUser()+"%");
		}
		if(StringUtils.isNotBlank(dto.getLogSuccess())) {
			criteria.andLogSuccessEqualTo(dto.getLogSuccess());
		}
		if((StringUtils.isNotBlank(dto.getStartTime()) && StringUtils.isNotBlank(dto.getEndTime())) && (Long.valueOf(TimeUtil.format(TimeUtil.parse(dto.getStartTime(), TimeUtil.FORMAT_SHORT), TimeUtil.FORMAT_INT_DATE))<Long.valueOf(TimeUtil.format(TimeUtil.parse(dto.getEndTime(), TimeUtil.FORMAT_SHORT), TimeUtil.FORMAT_INT_DATE)))) {
			criteria.andLogTimeBetween(TimeUtil.parse(dto.getStartTime(), TimeUtil.FORMAT_SHORT), TimeUtil.parse(dto.getEndTime(), TimeUtil.FORMAT_SHORT));
		}
		PageHelper.startPage(param.getPage(), param.getRows());
		List<SysLogHandle> list = sysLogHandleMapper.selectByExample(example);
		int countByExample = sysLogHandleMapper.countByExample(example);
		for (SysLogHandle sysLogHandle : list) {
			JSONObject object = new JSONObject();
			object.put("logId", sysLogHandle.getLogId());
			object.put("logMethed", sysLogHandle.getLogMethed());
			object.put("logUrl", sysLogHandle.getLogUrl());
			object.put("logIp", sysLogHandle.getLogIp());
			object.put("logUser", sysLogHandle.getLogUser());
			object.put("logTime", TimeUtil.format(sysLogHandle.getLogTime()));
			object.put("logSuccess", DictUtil.load_dict(sysLogHandle.getLogSuccess(),"LOG_STATE").getDispaly());
			
			array.add(object);
		}
		GridData data = new GridData();
		data.setTotal(countByExample);
		data.setRows(array);
		return GridData.gridToJson(data);
	}

	@Override
	public SysOplogDto load_01(SysOplogDto dto) {
		SysLogHandle logHandle = sysLogHandleMapper.selectByPrimaryKey(Long.valueOf(dto.getLogId()));
		AppUtils.copyProperties(dto, logHandle);
		dto.setLogTime(TimeUtil.format(logHandle.getLogTime(), TimeUtil.FORMAT_LONG));
		return dto;
	}

}
